<script>
import { ref, onMounted, onUnmounted } from 'vue'

// 内置图片资源
const defaultImages = [
  {
    src: 'src\\assets\\法律援助封面1.png',
    alt: '山脉风景'
  },
  {
    src: 'src\\assets\\法律援助封面2.png',
    alt: '森林风景'
  },
  {
    src: 'src\\assets\\法律援助封面3.png',
    alt: '峡谷风景'
  },
  {
    src: 'src\\assets\\法律援助封面4.png',
    alt: '666风景'
  }
]

export default {
  props: {
    images: {
      type: Array,
      default: () => defaultImages,
      validator: (value) => value.every(item => typeof item.src === 'string' && typeof item.alt === 'string')
    },
    interval: {
      type: Number,
      default: 3000
    },
    borderRadius: {
      type: String,
      default: '16px'
    }
  },
  setup(props) {
    const currentIndex = ref(0)
    let timer = null

    const next = () => {
      currentIndex.value = (currentIndex.value + 1) % props.images.length
    }

    const prev = () => {
      currentIndex.value = (currentIndex.value - 1 + props.images.length) % props.images.length
    }

    const goTo = (index) => {
      currentIndex.value = index
    }

    const startAutoPlay = () => {
      timer = setInterval(next, props.interval)
    }

    const stopAutoPlay = () => {
      if (timer) {
        clearInterval(timer)
        timer = null
      }
    }

    onMounted(() => {
      startAutoPlay()
    })

    onUnmounted(() => {
      stopAutoPlay()
    })

    return {
      currentIndex,
      next,
      prev,
      goTo
    }
  }
}
</script>

<template>
  <div class="home">
    <h1>欢迎来到法律援助系统</h1>
    <p>我们为需要法律帮助的人提供免费咨询服务</p>
    <div class="carousel-container">
      <div class="carousel">
        <div class="carousel-inner" :style="{ borderRadius }">
          <transition-group name="slide-fade" tag="div">
            <img v-for="(image, index) in images" v-show="currentIndex === index" :key="index" :src="image.src"
              :alt="image.alt" />
          </transition-group>
        </div>
        <button class="nav-btn prev" @click="prev">&lt;</button>
        <button class="nav-btn next" @click="next">&gt;</button>
      </div>
      <div class="indicators">
        <span v-for="(_, index) in images" :key="index" :class="{ active: currentIndex === index }"
          @click="goTo(index)"></span>
      </div>
    </div>
    <div class="features">
      <div class="feature-card">
        <h2>在线咨询</h2>
        <p>专业律师在线解答您的法律问题</p>
      </div>
      <div class="feature-card">
        <h2>案例库</h2>
        <p>查阅各类法律案例，了解类似情况处理</p>
      </div>
      <div class="feature-card">
        <h2>法律资源</h2>
        <p>获取最新法律法规和实用法律文书</p>
      </div>
    </div>
  </div>

</template>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
}

.features {
  display: flex;
  justify-content: space-between;
  margin-top: 2rem;
  flex-wrap: wrap;
}

.feature-card {
  flex: 1;
  min-width: 300px;
  margin: 1rem;
  padding: 1.5rem;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.feature-card h2 {
  color: #2c3e50;
  margin-bottom: 1rem;
}

@media (max-width: 768px) {
  .features {
    flex-direction: column;
  }
}



.carousel-container {
  position: relative;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}

.carousel {
  position: relative;
  height: 400px;
}

.carousel-inner {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.carousel img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.3);
  color: white;
  border: none;
  border-radius: 50%;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
  z-index: 10;
}

.nav-btn:hover {
  background-color: rgba(0, 0, 0, 0.5);
}

.prev {
  left: 20px;
}

.next {
  right: 20px;
}

.indicators {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 15px;
}

.indicators span {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background-color: #ccc;
  cursor: pointer;
  transition: background-color 0.3s;
}

.indicators span.active {
  background-color: #333;
}

/* 动画效果 */
.slide-fade-enter-active,
.slide-fade-leave-active {
  transition: all 0.5s ease;
}

.slide-fade-enter-from {
  opacity: 0;
  transform: translateX(100%);
}

.slide-fade-leave-to {
  opacity: 0;
  transform: translateX(-100%);
}

.slide-fade-enter-to,
.slide-fade-leave-from {
  opacity: 1;
  transform: translateX(0);
}
</style>